package com.g001.model.powers;

import org.newdawn.slick.Font;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.StateBasedGame;

import com.g001.enums.BlockType;
import com.g001.enums.MessagePositionType;
import com.g001.util.CachedResourceLoader;

public class MessageAction extends Action {
	
	private MessagePositionType messagePositionType;
	private String text;
	private String text2;	
	private Long currentTime;
	private Vector2f position;
	private float widthText;
	private float heightText;
	private long DEFAULT_DURATION = 1000l;
	private long duration;
	private float angle;
		
	public final static int BIG_FONT = 2;
	public final static int NORMAL_FONT = 1;
	public final static int SMALL_FONT = 0;
	
	private Font font;
	
	public MessageAction(final String text,MessagePositionType messagePositionType, long duration) {
		init(text, messagePositionType, duration,NORMAL_FONT);
	}
	
	public MessageAction(final String text,MessagePositionType messagePositionType) {
		 init(text, messagePositionType, DEFAULT_DURATION,NORMAL_FONT);
	}
	
	public MessageAction(final String text,MessagePositionType messagePositionType,long duration,int fontSize) {
		 init(text, messagePositionType, duration,fontSize);
	}
	
	private void init(String text, MessagePositionType messagePositionType,
			long duration, int fontSize) {
		this.duration = duration;
		this.messagePositionType = messagePositionType;
		this.text = text;
		font = CachedResourceLoader.font;
		if(fontSize == BIG_FONT) { font = CachedResourceLoader.fontBig;}
		if(fontSize == SMALL_FONT) { font = CachedResourceLoader.fontSmall;}
		widthText = font.getWidth(text);
		heightText = font.getHeight(text);
		currentTime = duration;
		
		angle = 0;
		switch(messagePositionType){
			case LEFT :
			case RIGHT: angle=-5;break;
			default : break;
		}
		type = BlockType.BORDER;
	}
	
	public void addLine2(final String text2){
		this.text2 = text2;
	}

	public void render(GameContainer container, StateBasedGame game, Graphics g)
			throws SlickException {
		if(position!=null)
		{
			g.setAntiAlias(false);
			g.rotate(position.x-widthText/2, position.y-heightText/2, angle);
			font.drawString(position.x, position.y, text);
			if(text2!=null) {
				font.drawString(position.x, position.y+heightText, text2);
			}
			g.rotate(position.x-widthText/2, position.y-heightText/2, -1*angle);
			g.resetTransform();
			
		}
	}

	public void update(GameContainer container, StateBasedGame game, int delta)
			throws SlickException {
		switch(messagePositionType){
			case CENTER : position = new Vector2f((container.getWidth() - widthText)/2, (container.getHeight()-heightText)/2 - 50);break;
			case LEFT : position = new Vector2f((200-widthText)/2 , 250 );break;
			//case RIGHT : position = new Vector2f(container.getWidth() - 150 - widthText/2, 300 );break;
			case RIGHT : position = new Vector2f(container.getWidth() - 200 +(200-widthText)/2, 250 );break;
			case BOTTOM : position = new Vector2f((container.getWidth() - widthText)/2, heightText -100);break;
			default :
				position = new Vector2f((container.getWidth() - widthText)/2, (container.getHeight()-heightText)/2 - 50);break;
		}
	}
	 // true when the animation is over
	public boolean isFinished(){
		return currentTime <= System.currentTimeMillis() && duration>0;
	}

	@Override
	public void action() {
		this.currentTime = System.currentTimeMillis() + duration;
	}

	@Override
	public void setBonus(int bonus) {
		this.bonus = bonus;
	}
	
	 

}

